package com.example.tms.controller;

import com.example.tms.bean.DriverInfo;
import com.example.tms.bean.DriverView;
import com.example.tms.bean.ReturnInfo;
import com.example.tms.mapper.*;
import com.example.tms.tools.GlobalParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class DriverController {

    private final DriverMapper driverMapper;
    private final DriverVMapper driverVMapper;
    private final DeptMapper deptMapper;
    private final CarMapper carMapper;
    private final FileMapper fileMapper;

    @Autowired
    public DriverController(DriverMapper driverMapper, DriverVMapper driverVMapper, DeptMapper deptMapper, CarMapper carMapper, FileMapper fileMapper){
        this.driverMapper = driverMapper;
        this.driverVMapper = driverVMapper;
        this.deptMapper = deptMapper;
        this.carMapper = carMapper;
        this.fileMapper = fileMapper;
    }

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    //查询司机信息列表
    @RequestMapping(value = "driver", method = RequestMethod.GET)
    public ReturnInfo getDeptInfoAll(@RequestParam(required = false, defaultValue = "") String type,
                                     @RequestParam(required = false, defaultValue = "") String value) {
        List<DriverView> result = driverVMapper.findAll((Root<DriverView> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder)->{
            List<Predicate> list = new ArrayList<>();
            switch (type) {
                case "jsyxm":
                    list.add(criteriaBuilder.like(root.get("jsyxm").as(String.class), "%" + value + "%"));
                    break;
                case "zzzt":
                    list.add(criteriaBuilder.like(root.get("zzzt").as(String.class), "%" + value + "%"));
                    break;
                case "name":
                    list.add(criteriaBuilder.like(root.get("name").as(String.class),"%" + value + "%"));
                    break;
                default:
                    break;
            }
            list.add(criteriaBuilder.equal(root.get("sfcz").as(Boolean.class), true));
            Predicate[] p = new Predicate[list.size()];
            criteriaQuery.where(criteriaBuilder.and(list.toArray(p)));
            criteriaQuery.orderBy(criteriaBuilder.desc(root.get("lasttime")));
            return criteriaQuery.getRestriction();
        });
        return ReturnInfo.info2(result);
    }

    //查询司机信息（司机id）
    @RequestMapping(value = "driver/{sjid}", method = RequestMethod.GET)
    public DriverView getDriverinfo(@PathVariable String sjid) {
        return driverVMapper.findBySjidAndSfcz(sjid,true);
    }

    //新建司机信息
    @RequestMapping(value = "driver", method = RequestMethod.POST)
    public List<Map<String,Object>> saveDrivernfo(@RequestBody DriverInfo driverInfo){
        List<Map<String,Object>>  list = new ArrayList<>();
        Map<String,Object>  map = new HashMap<>();
        if (driverInfo.getJsyxm().equals("")|| !checkJsyxm(driverInfo.getJsyxm())) {
            map.put("jsyxm", "0");
        }
        if(driverInfo.getBumenid().equals("")){
            map.put("bumenid", "0");
        }else if(deptMapper.findByBumenid(driverInfo.getBumenid())==null){
            map.put("bumenid", "1");
        }
        if(!driverInfo.getGangwei().equals("")&&!checkGangwei(driverInfo.getGangwei())){
            map.put("gangwei","0");
        }
        if(driverInfo.getJsysj().equals("")||!checkJsysjh(driverInfo.getJsysj())){
            map.put("jsysj","0");
        }else if(driverMapper.findByJsysjAndSfcz(driverInfo.getJsysj(),true)!=null){
            map.put("jsysj", "2");
        }
        if (driverInfo.getSfzh().equals("") || !checkSfzh(driverInfo.getSfzh())){
            map.put("sfzh", "0");
        }else if(driverMapper.findBySfzhAndSfcz(driverInfo.getSfzh(),true)!=null){
            map.put("sfzh", "2");
        }
        if(!driverInfo.getHtbh().equals("")&&!checkHtbh(driverInfo.getHtbh())){
            map.put("htbh", "0");
        }
        if(driverInfo.getSfzszd().equals("")||!checkSfzszd(driverInfo.getSfzszd())){
            map.put("sfzszd", "0");
        }
        if(!driverInfo.getClid().equals("")&&carMapper.findByClid(driverInfo.getClid())==null){
            map.put("clid", "1");
        }
        if(!driverInfo.getCyzgzfzjg().equals("")&&!checkCyzgzfzjg(driverInfo.getCyzgzfzjg())){
            map.put("cyzgzfzjg", "0");
        }
        if(!driverInfo.getYyzfzjg().equals("")&&!checkYyzfzjg(driverInfo.getYyzfzjg())){
            map.put("yyzfzjg", "0");
        }
        if(!driverInfo.getJszfzjg().equals("")&&!checkJszfzjg(driverInfo.getJszfzjg())){
            map.put("jszfzjg", "0");
        }
        if(!driverInfo.getYjscph().equals("")&&!checkYjscph(driverInfo.getYjscph())){
            map.put("yjscph", "0");
        }
        if(!driverInfo.getZzzt().equals("")&&!checkZzzt(driverInfo.getZzzt())){
            map.put("zzzt", "0");
        }
        if(!driverInfo.getYyzh().equals("")&&!checkYyzh(driverInfo.getYyzh())){
            map.put("yyzh", "0");
        }
        if (driverInfo.getJszh().equals("") || !checkJszh(driverInfo.getJszh())){
            map.put("jszh", "0");
        }
        if (driverInfo.getDah().equals("")|| !checkDah(driverInfo.getDah())){
            map.put("dah", "0");
        }
        if (driverInfo.getJszclrq() == null){
            map.put("jszclrq", "0");
        }
        if (driverInfo.getJszyxqz() == null){
            map.put("jszyxqz", "0");
        }
        if (driverInfo.getCyzgzh().equals("")|| !checkCyzgzh(driverInfo.getCyzgzh())){
            map.put("cyzgzh", "0");
        }
        if (driverInfo.getCyzgzclrq() == null){
            map.put("cyzgzclrq", "0");
        }
        if (driverInfo.getCyzgzyxqz() == null){
            map.put("cyzgzyxqz", "0");
        }
        if(!driverInfo.getSfzfyj().equals("")&&fileMapper.findOne(driverInfo.getSfzfyj())==null){
            map.put("sfzfyj", "1");
        }
        if(!driverInfo.getJkzmzp().equals("")&&fileMapper.findOne(driverInfo.getJkzmzp())==null){
            map.put("jkzmzp", "1");
        }
        if(!driverInfo.getJszfyj().equals("")&&fileMapper.findOne(driverInfo.getJszfyj())==null){
            map.put("jszfyj", "1");
        }
        if(!driverInfo.getCyzgzfyj().equals("")&&fileMapper.findOne(driverInfo.getCyzgzfyj())==null){
            map.put("cyzgzfyj", "1");
        }
        if(!driverInfo.getYyzfyj().equals("")&&fileMapper.findOne(driverInfo.getYyzfyj())==null){
            map.put("yyzfyj", "1");
        }
        list.add(map);
        if(map.isEmpty()){
            if(!driverInfo.getSfzfyj().equals("")){
                fileMapper.updateCz(driverInfo.getSfzfyj());
            }
            if(!driverInfo.getJkzmzp().equals("")){
                fileMapper.updateCz(driverInfo.getJkzmzp());
            }
            if(!driverInfo.getJszfyj().equals("")){
                fileMapper.updateCz(driverInfo.getJszfyj());
            }
            if(!driverInfo.getCyzgzfyj().equals("")){
                fileMapper.updateCz(driverInfo.getCyzgzfyj());
            }
            if(!driverInfo.getYyzfyj().equals("")){
                fileMapper.updateCz(driverInfo.getYyzfyj());
            }
            driverInfo.setLasttime(GlobalParam.getTime());
            driverInfo.setSfcz(true);
            driverMapper.save(driverInfo);
            logger.info("新建司机"+driverInfo.toString());
        }
        return list;
    }

    //更新司机信息
    @RequestMapping(value = "driver", method = RequestMethod.PUT)
    public List<Map<String,Object>> updateDriverinfo(@RequestBody DriverInfo driverInfo){
        List<Map<String,Object>>  list = new ArrayList<>();
        Map<String,Object>  map = new HashMap<>();
        if (driverInfo.getSjid()==null||driverInfo.getSjid().equals("")) {
            map.put("sjid", "0");
        }else if(driverMapper.findBySjid(driverInfo.getSjid()) == null){
            map.put("sjid", "1");
        }
        DriverInfo oldDriver = driverMapper.findBySjid(driverInfo.getSjid());
        if (driverInfo.getJsyxm().equals("")|| !checkJsyxm(driverInfo.getJsyxm())) {
            map.put("jsyxm", "0");
        }
        if(driverInfo.getBumenid().equals("")){
            map.put("bumenid", "0");
        }else if(deptMapper.findByBumenid(driverInfo.getBumenid())==null){
            map.put("bumenid", "1");
        }
        if(!driverInfo.getGangwei().equals("")&&!checkGangwei(driverInfo.getGangwei())){
            map.put("gangwei","0");
        }
        if(driverInfo.getJsysj().equals("")||!checkJsysjh(driverInfo.getJsysj())){
            map.put("jsysj","0");
        }else if(!driverInfo.getJsysj().equals(oldDriver.getJsysj())&&driverMapper.findByJsysjAndSfcz(driverInfo.getJsysj(),true)!=null){
            map.put("jsysj", "2");
        }
        if (driverInfo.getSfzh().equals("") || !checkSfzh(driverInfo.getSfzh())){
            map.put("sfzh", "0");
        }else if(!driverInfo.getSfzh().equals(oldDriver.getSfzh())&&driverMapper.findBySfzhAndSfcz(driverInfo.getSfzh(),true)!=null){
            map.put("sfzh", "2");
        }
        if(driverInfo.getSfzszd().equals("")||!checkSfzszd(driverInfo.getSfzszd())){
            map.put("sfzszd", "0");
        }
        if(!driverInfo.getClid().equals("")&&carMapper.findByClid(driverInfo.getClid())==null){
            map.put("clid", "1");
        }
        if(!driverInfo.getCyzgzfzjg().equals("") && !checkCyzgzfzjg(driverInfo.getCyzgzfzjg())){
            map.put("cyzgzfzjg", "0");
        }
        if(!driverInfo.getYyzfzjg().equals("") && !checkYyzfzjg(driverInfo.getYyzfzjg())){
            map.put("yyzfzjg", "0");
        }
         if(!driverInfo.getJszfzjg().equals("") && !checkJszfzjg(driverInfo.getJszfzjg())){
            map.put("jszfzjg", "0");
        }
        if(!driverInfo.getYjscph().equals("")&&!checkYjscph(driverInfo.getYjscph())){
            map.put("yjscph", "0");
        }
        if(!driverInfo.getZzzt().equals("")&&!checkZzzt(driverInfo.getZzzt())){
            map.put("zzzt", "0");
        }
        if(!driverInfo.getHtbh().equals("")&&!checkHtbh(driverInfo.getHtbh())){
            map.put("htbh", "0");
        }
        if (driverInfo.getJszh().equals("") || !checkJszh(driverInfo.getJszh())){
            map.put("jszh", "0");
        }
        if (driverInfo.getDah().equals("") || !checkDah(driverInfo.getDah())){
            map.put("dah", "0");
        }
        if (driverInfo.getJszclrq() == null){
            map.put("jszclrq", "0");
        }
        if (driverInfo.getJszyxqz() == null){
            map.put("jszyxqz", "0");
        }
        if(!driverInfo.getYyzh().equals("")&&!checkYyzh(driverInfo.getYyzh())){
            map.put("yyzh", "0");
        }
        if (driverInfo.getCyzgzh().equals("") || !checkCyzgzh(driverInfo.getCyzgzh())){
            map.put("cyzgzh", "0");
        }
        if (driverInfo.getCyzgzclrq() == null){
            map.put("cyzgzclrq", "0");
        }
        if (driverInfo.getCyzgzyxqz() == null){
            map.put("cyzgzyxqz", "0");
        }
        if(!driverInfo.getSfzfyj().equals("")&&fileMapper.findOne(driverInfo.getSfzfyj())==null){
            map.put("sfzfyj", "1");
        }
        if(!driverInfo.getJkzmzp().equals("")&&fileMapper.findOne(driverInfo.getJkzmzp())==null){
            map.put("jkzmzp", "1");
        }
        if(!driverInfo.getJszfyj().equals("")&&fileMapper.findOne(driverInfo.getJszfyj())==null){
            map.put("jszfyj", "1");
        }
        if(!driverInfo.getCyzgzfyj().equals("")&&fileMapper.findOne(driverInfo.getCyzgzfyj())==null){
            map.put("cyzgzfyj", "1");
        }
        if(!driverInfo.getYyzfyj().equals("")&&fileMapper.findOne(driverInfo.getYyzfyj())==null){
            map.put("yyzfyj", "1");
        }
        list.add(map);
        if(map.isEmpty()){
            if(!driverInfo.getSfzfyj().equals("")&&!fileMapper.findOne(driverInfo.getSfzfyj()).getSfcz()){
                fileMapper.updateCz(driverInfo.getSfzfyj());
            }
            if(!driverInfo.getJkzmzp().equals("")&&!fileMapper.findOne(driverInfo.getJkzmzp()).getSfcz()){
                fileMapper.updateCz(driverInfo.getJkzmzp());
            }
            if(!driverInfo.getJszfyj().equals("")&&!fileMapper.findOne(driverInfo.getJszfyj()).getSfcz()){
                fileMapper.updateCz(driverInfo.getJszfyj());
            }
            if(!driverInfo.getCyzgzfyj().equals("")&&!fileMapper.findOne(driverInfo.getCyzgzfyj()).getSfcz()){
                fileMapper.updateCz(driverInfo.getCyzgzfyj());
            }
            if(!driverInfo.getYyzfyj().equals("")&&!fileMapper.findOne(driverInfo.getYyzfyj()).getSfcz()){
                fileMapper.updateCz(driverInfo.getYyzfyj());
            }
            driverInfo.setLasttime(GlobalParam.getTime());
            driverInfo.setSfcz(true);
            driverMapper.saveAndFlush(driverInfo);
            logger.info("更新司机"+driverInfo.toString());
        }
            return list;
    }

    //删除司机信息（假删除 司机id）
    @RequestMapping(value = "driver/{sjid}", method = RequestMethod.DELETE)
    public ReturnInfo deleteDriverinfo(@PathVariable String sjid) {
        String b;
        if (driverMapper.findBySjid(sjid) == null) {
            b = "删除失败！";
        } else {
            driverMapper.deleteBySjid(sjid);
            b = "删除成功！";
            logger.info("删除司机"+sjid);
        }
        return ReturnInfo.info1(b);
    }

    //检查姓名格式
    private Boolean checkJsyxm(String jsyxm){
        Boolean b = false;
        if(jsyxm.matches("^[\\u4E00-\\u9FA5A-Za-z]+$")){
            b = true;
        }
        return b;
    }
    //检查岗位格式
    private Boolean checkGangwei(String gangwei){
        Boolean b = false;
        if(gangwei.equals("主驾")||gangwei.equals("副驾")){
            b = true;
        }
        return b;
    }
    //检查驾驶员手机号格式
    private Boolean checkJsysjh(String jsysjh){
        Boolean b = false;
        if(jsysjh.matches("^1[0-9 ]{10,12}$")){
            b = true;
        }
        return b;
    }
    //检查身份证号格式
    private Boolean checkSfzh(String sfzh) {
        Boolean b = false;
        if(sfzh.matches("^(\\d{15}$|^\\d{18}$|^\\d{17}(\\d|X|x))$")){
            b = true;
        }
        return b;
    }
    //检查客户身份证所在地格式
    private Boolean checkSfzszd(String sfzszd) {
        Boolean b = false;
        if(sfzszd.matches("^.{1,30}$")){
            b = true;
        }
        return b;
    }
    //检查合同编号格式
    private Boolean checkHtbh(String htbh) {
        Boolean b = false;
        if(htbh.matches("^.{1,10}$")){
            b = true;
        }
        return b;
    }
    //检查发证机关格式
    private Boolean checkCyzgzfzjg(String cyzgzfzjg) {
        Boolean b = false;
        if(cyzgzfzjg.matches("^.{1,20}$")){
            b = true;
        }
        return b;
    }
    //检查发证机关格式
    private Boolean checkJszfzjg(String jszfzjg) {
        Boolean b = false;
        if(jszfzjg.matches("^.{1,20}$")){
            b = true;
        }
        return b;
    }
    //检查发证机关格式
    private Boolean checkYyzfzjg(String yyzfzjg) {
        Boolean b = false;
        if(yyzfzjg.matches("^.{1,20}$")){
            b = true;
        }
        return b;
    }
    //检查档案号格式
    private Boolean checkDah(String dah) {
        Boolean b = false;
        if(dah.matches("^\\d{1,20}$")){
            b = true;
        }
        return b;
    }
    //检查从业资格证号格式
    private Boolean checkCyzgzh(String cyzgzh) {
        Boolean b = false;
        if(cyzgzh.matches("^\\d{1,20}$")){
            b = true;
        }
        return b;
    }
    //检查押运证号格式
    private Boolean checkYyzh(String yyzh) {
        Boolean b = false;
        if(yyzh.matches("^\\d{1,20}$")){
            b = true;
        }
        return b;
    }
    //检查驾驶证号格式
    private Boolean checkJszh(String  jszh){
        Boolean b = false;
        if(jszh.matches("^(\\d{15}$|^\\d{18}$|^\\d{17}(\\d|X|x))$")){
            b = true;
        }
        return b;
    }
    //检查在职状态格式
    private Boolean checkZzzt(String zzzt){
        Boolean b = false;
        if(zzzt.equals("在职")||zzzt.equals("离职")){
            b = true;
        }
        return b;
    }

    //检查原驾驶车牌号格式
    private Boolean checkYjscph(String yjscph){
        Boolean b = false;
        if(yjscph.matches("^[冀豫云辽黑湘皖鲁苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼渝京津沪新京军空海北沈兰济南广成使领A-Z]{1}[a-zA-Z0-9]{5}[a-zA-Z0-9挂学警港澳]{1}$")){
            b = true;
        }
        return b;
    }

}